import { ref, reactive, onMounted } from 'vue';
import { getAddress } from '@/api/api'
import router from '@/router/index'
import { useAddressStore } from '@/store/index'
import { usePayStore } from '@/store/goPay'

//地址的数据的hooks
export function useProvide() {
    //地址数据

    const list = ref([])
    //请求地址数据
    
    async function Rendeaddress() {
        const res = await getAddress({ id: 11 })
        // console.log(res);
        list.value = res.list

    }
    //初始渲染一遍
    Rendeaddress()




    //头部路由切换 步骤
    const flag = ref(2)
    const flagone = () => {
        flag.value = 1
        router.replace('/shop')
    }
    const flagtwo = () => {
        flag.value = 2

    }

    //头部地区位置切换(列如：北京)
    const value = ref(0);
    const option = [
        { text: '北京', value: 0 },
        { text: '上海', value: 1 },
        { text: '深圳', value: 2 },
    ]

    //点击跳转到购买订单

    const AddOrder = (id) => {
        //存储地址到pinia  定义一个队象
        list.value.filter(function(item) {
            if (item.addressId == id) {
                const addressinfos = reactive({
                    Labelress: item.Labelress,
                    address: item.address,
                    addressDetail: item.addressDetail,
                    userName: item.name,
                    userPhone: item.tel,
                    is_sex: item.sex

                })
                //哪里用到定义到哪里 不然全局挂载会报错
                let { setAddress } = useAddressStore()
                let { editaddress } = usePayStore()
                setAddress(addressinfos)
                editaddress(addressinfos)
                router.push('/confirmOrder')
            }

        })
    }

    //点击跳转到添加地址
    const toaddressList = () => {
        router.push('./address')
    }

    const tomyaddress = () => {
        router.push('/myaddress')
    }

    return {
        list, flag, flagone, flagtwo, value, option, AddOrder, toaddressList, tomyaddress, Rendeaddress
    }
}
